package com.study.web.context;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.study.common.constant.WebConstant;
import com.study.web.aop.ResponseTimeAop;
import com.study.web.entity.SysParams;

@SuppressWarnings("all")
@Component
public class SpringContextUtil implements ApplicationContextAware {
	Logger logger = Logger.getLogger(getClass());
	private static ApplicationContext applicationContext;
	
	/**
	 * 设置上下文环境
	 */
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.applicationContext = context;
		//ResponseTimeAop bean = getApplicationContext().getBean(ResponseTimeAop.class);
		//System.out.println("ResponseTimeAop_------------->" + bean);
	}
	/**
	 * 获取上下文环境
	 * @return
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}
	/**
	 * 根据name获取bean
	 * @param name
	 * @return
	 * @throws BeansException
	 */
	public static Object getBean(String name) throws BeansException {
		try {
			return getApplicationContext().getBean(name);
		} catch (Exception e) {
			throw new RuntimeException("获取的Bean不存在！");
		}
	}
	/**
	 * 获取bean泛型
	 * @param name
	 * @param requiredType
	 * @return
	 * @throws BeansException
	 */
	public static <T> T getBean(String name, Class<T> requiredType)
			throws BeansException {
		return getApplicationContext().getBean(name, requiredType);
	}
	/**
	 * 是否包含某个bean
	 * @param name
	 * @return
	 */
	public static boolean containsBean(String name) {
		return getApplicationContext().containsBean(name);
	}
	/**
	 * 查询bean是否是单例模式
	 * @param name
	 * @return
	 * @throws NoSuchBeanDefinitionException
	 */
	public static boolean isSingleton(String name)	throws NoSuchBeanDefinitionException {
		return getApplicationContext().isSingleton(name);
	}
	/**
	 * 获取类型
	 * @param name
	 * @return
	 * @throws NoSuchBeanDefinitionException
	 */
	public static Class<? extends Object> getType(String name)	throws NoSuchBeanDefinitionException {
		return getApplicationContext().getType(name);
	}
	/**
	 * 获取别名数组
	 * @param name
	 * @return
	 * @throws NoSuchBeanDefinitionException
	 */
	public static String[] getAliases(String name)	throws NoSuchBeanDefinitionException {
		return getApplicationContext().getAliases(name);
	}
	/**
	 * 替换敏感字符方法
	 */
	public static String replaceSensitiveWord(String content) throws Exception{
		RedisTemplate redisTemplate = (RedisTemplate) getBean("redisTemplate");
		SysParams  sensitiveWordEntity = (SysParams) redisTemplate.opsForValue().get(WebConstant.SYS_PARAMS + "sensitive_word");
		if(sensitiveWordEntity != null){
			String value = sensitiveWordEntity.getKeyValue();
			if(!StringUtils.isEmpty(value)){
				for(String s : value.split(",")){
					content = content.replaceAll(s, "");
					content = content.replaceAll(s.toLowerCase(), "");
					content = content.replaceAll(s.toUpperCase(), "");
				}
			}
		}
		return content;
	}

	/**
	 * 查找敏感字符
	 * 如果存在则返回true
	 */
	public static boolean findSensitiveWord(String content){
		RedisTemplate redisTemplate = (RedisTemplate) getBean("redisTemplate");
		SysParams  sensitiveWordEntity = (SysParams) redisTemplate.opsForValue().get(WebConstant.SYS_PARAMS + "sensitive_word");
		if(sensitiveWordEntity != null){
			String value = sensitiveWordEntity.getKeyValue();
			if(!StringUtils.isEmpty(value)){
				for(String s : value.split(",")){
					if(content.contains(s) || content.contains(s.toLowerCase()) || content.contains(s.toUpperCase())){
						return true;
					}
				}
			}
		}
		return false;
	}
	/**
	 * 超级管理员用户名
	 */
	private static String superAdministrator = "admin";
	/**
	 * 超级管理员的角色Id
	 */
	private static Long superAdministratorRoleId = 1L;
	/**
	 * 超级管理员的用户Id
	 */
	private static Long superAdministratorUserId = 1L;
	/**
	 * 获取超级管理用户登录名
	 */
	public static String getSuperAdministratorLoginName(){
		return superAdministrator;
	}
	/**
	 * 获取超级管理员的角色Id
	 */
	public static Long getSuperAdministratorRoleId(){
		return superAdministratorRoleId;
	}
	/**
	 * 获取超级管理员的用户Id
	 */
	public static Long getSuperAdministratorUserId(){
		return superAdministratorUserId;
	}
}
